import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:star_author/config/app.dart';
import 'package:star_author/generated/json/base/json_convert_content.dart';
import 'package:star_author/utils/common_util/shared_preferences/sp_util.dart';
import 'package:star_author/utils/http/api_manager.dart';
import 'package:star_author/utils/http/http_util.dart';
import 'package:star_author/widgets/dialog/notice_dialog/model/notice_entity.dart';

import '../../../utils/common_util/shared_preferences/sp_keys_constant.dart';
import 'notice.dart';

class NoticeDialogHandler {
  //最后一条公告
  static late NoticeData data = NoticeData();

  static bool announcement_read = false;

  //todo://公告弹框
  void showNoticeDialog(context, {Function? nextCallback}) async {
    bool? isAgree = SpUtil.getBool(App.agree);
    if (isAgree!) {
      HttpUtil.commonRequest(
        context,
        ApiManager.announcements,
        isLoading: false,
        isErrorToast: false,
      ).then((value) async {
        //获取后台的公告集合
        NoticeEntity? entity =
            JsonConvert.fromJsonAsT<NoticeEntity>(value.data);
        //已经阅读的公告集合
        List<String>? list =
            SpUtil.getStringList(SpKeysConstant.notice, defValue: []);
        List<NoticeData> diffList = [];
        if (entity?.data != null && (entity?.data?.length ?? 0) > 0) {
          NoticeData noticeData = entity?.data?.last ?? NoticeData();
          //服务器的id和本地判断 是否需要添加
          int indexWhere = getId(noticeData.id.toString(), list!);
          if (indexWhere == -1 && (noticeData.effective ?? false)) {
            //本地服务器没有当前公告id 需要展示
            diffList.add(noticeData);
          }
          if (diffList.length > 0) {
            Get.dialog(
              Notice(
                //只需要显示最后一条公告
                data: [diffList.last],
                localIds: list,
                nextCallback: nextCallback,
              ),
              barrierDismissible: true, //是否可以通过空白点击
              barrierColor: Colors.black54.withOpacity(0.3),
            );
          } else {
            nextCallback?.call();
          }
        } else {
          nextCallback?.call();
        }
      }).catchError((e) {
        nextCallback?.call();
      });
    } else {
      nextCallback?.call();
    }
  }

  int getId(id, List list) {
    return list.indexWhere((element) => element == id);
  }

  static getData() async {
    await HttpUtil.commonRequest(
      Get.context!,
      ApiManager.announcements,
      isLoading: false,
      isErrorToast: false,
    ).then((value) {
      //获取后台的公告集合
      NoticeEntity? entity = JsonConvert.fromJsonAsT<NoticeEntity>(value.data);
      //获取最后一条数据
      NoticeData noticeData;
      if (entity != null &&
          entity.data != null &&
          entity.data!.isNotEmpty &&
          entity.data?.last != null) {
        noticeData = entity.data?.last ?? NoticeData();
      } else {
        noticeData = NoticeData();
      }
      if ((noticeData.effective ?? false)) {
        data = noticeData;
        announcement_read = entity?.announcement_read ?? false;
      } else {
        data = NoticeData();
        announcement_read = true;
      }
    });
  }
}
